
package main;

import model.Student;
import model.Teacher;
import model.Course;
import model.Score;
import Service.GradeManagementSystem;
import exception.ScoreException;

import java.util.Scanner;

public class Main {
    private static GradeManagementSystem system = new GradeManagementSystem();
    private static Scanner scanner = new Scanner(System.in);

    public static void main(String[] args) {
        System.out.println("=== 欢迎进入成绩管理系统 ===");

        // Display all options directly in the main screen
        while (true) {
            System.out.println("请选择操作:");
            System.out.println("1. 查询学生GPA");
            System.out.println("2. 录入学生成绩");
            System.out.println("3. 查询课程成绩");
            System.out.println("4. 添加学生");
            System.out.println("5. 添加教师");
            System.out.println("6. 添加课程");
            System.out.println("7. 退出");

            int choice = scanner.nextInt();
            scanner.nextLine();  // Consume newline character

            switch (choice) {
                case 1:
                    queryStudentGPA();
                    break;
                case 2:
                    inputStudentScore();
                    break;
                case 3:
                    queryTeacherCourseScores();
                    break;
                case 4:
                    addStudent();
                    break;
                case 5:
                    addTeacher();
                    break;
                case 6:
                    addCourse();
                    break;
                case 7:
                    System.out.println("退出系统！");
                    return;
                default:
                    System.out.println("无效选择，请重新选择！");
            }
        }
    }

    private static void queryStudentGPA() {
        System.out.print("请输入学生学号：");
        String studentId = scanner.nextLine();
        Student student = system.getStudents().get(studentId);

        if (student != null) {
            double gpa = system.calculateGPA(student);
            System.out.printf("学生 %s 的GPA为：%.2f%n", student.getName(), gpa);
        } else {
            System.out.println("学生不存在！");
        }
    }

    private static void inputStudentScore() {
        System.out.print("请输入学生学号：");
        String studentId = scanner.nextLine();
        Student student = system.getStudents().get(studentId);

        System.out.print("请输入课程编号：");
        String courseCode = scanner.nextLine();
        Course course = system.getCourses().get(courseCode);

        if (student != null && course != null) {
            System.out.print("请输入成绩：");
            double score = scanner.nextDouble();
            scanner.nextLine();  // Consume newline

            try {
                system.addScore(student, course, score);
                System.out.println("成绩录入成功！");
            } catch (ScoreException e) {
                System.out.println("成绩录入失败：" + e.getMessage());
            }
        } else {
            System.out.println("学生或课程不存在！");
        }
    }

    private static void queryTeacherCourseScores() {
        System.out.print("请输入课程编号：");
        String courseCode = scanner.nextLine();
        Course course = system.getCourses().get(courseCode);

        if (course != null) {
            for (Score score : system.getScoresByCourse(course)) {
                System.out.printf("学号：%s，姓名：%s，成绩：%.1f%n",
                        score.getStudent().getId(), score.getStudent().getName(), score.getScore());
            }
        } else {
            System.out.println("课程不存在！");
        }
    }

    private static void addStudent() {
        System.out.print("请输入学生ID：");
        String id = scanner.nextLine();
        System.out.print("请输入学生姓名：");
        String name = scanner.nextLine();
        System.out.print("请输入学生密码：");
        String password = scanner.nextLine();
        System.out.print("请输入学生班级：");
        String clazz = scanner.nextLine();

        system.addStudent(id, name, password, clazz);
        System.out.println("学生信息添加成功！");
    }

    private static void addTeacher() {
        System.out.print("请输入教师ID：");
        String id = scanner.nextLine();
        System.out.print("请输入教师姓名：");
        String name = scanner.nextLine();
        System.out.print("请输入教师密码：");
        String password = scanner.nextLine();
        System.out.print("请输入教师职称：");
        String title = scanner.nextLine();

        system.addTeacher(id, name, password, title);
        System.out.println("教师信息添加成功！");
    }

    private static void addCourse() {
        System.out.print("请输入课程编号：");
        String code = scanner.nextLine();
        System.out.print("请输入课程名称：");
        String name = scanner.nextLine();
        System.out.print("请输入课程学分：");
        int credit = scanner.nextInt();
        scanner.nextLine();  // Consume newline

        system.addCourse(code, name, credit);
        System.out.println("课程信息添加成功！");
    }
}
